package forum.client;
import java.util.Date;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.ClosingEvent;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

import forum.shared.Delimiters;
import forum.shared.ServerResponse;
/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ForumWeb implements EntryPoint {
	public static final int REFRESH_INTERVAL = 5000; // ms
	private FlexTable headerTable;
	private Label timeLbl;
	private Image image;
	
	
	public ForumWeb() {
		headerTable = new FlexTable();
		image = new Image("bannana.jpg");
		timeLbl = new Label();
		timeLbl.setText("Time: "+DateTimeFormat.getMediumDateTimeFormat().format(new Date()));
		
		headerTable.setWidget(0, 1, timeLbl);
		headerTable.setWidget(0, 0, image);
		
	    // Setup timer to refresh list automatically.
	    Timer refreshTimer = new Timer() {
		    @Override
		    public void run() {
		    	timeLbl.setText("Time: " +DateTimeFormat
		    			.getMediumDateTimeFormat().format(new Date()));
		    }
	    };
	    refreshTimer.scheduleRepeating(REFRESH_INTERVAL);
	}
	
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		new MainScreen().setupDisplayInMainPanel();
		Label errorMsgLabel = new Label();
		errorMsgLabel.setStyleName("errorMessage");
	    errorMsgLabel.setVisible(false);
	    MainPanel.get().add(errorMsgLabel);
	    
		// Associate the mainPanel with the HTML host page body.
	    
	    // the header is common to all screens
	    RootPanel.get().add(headerTable);
		
	    // content of each screen appears in MainPanel
	    RootPanel.get().add(MainPanel.get());
	    
	    // handle sending user status to server
	    handleUserPresence();
	}


	private void handleUserPresence() {
		final ForumServiceAsync forumSvc = GWT.create(ForumService.class);
		
		// send connect msg to server
		AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse>() {

			@Override
			public void onSuccess(ServerResponse result) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		};
		
		forumSvc.sendMsg("Connect", callback);
		
		// set to send disconnect msg on window close
	    Window.addWindowClosingHandler(new Window.ClosingHandler() {
			@Override
			public void onWindowClosing(ClosingEvent event) {
				// Set up the callback object.
			    AsyncCallback<ServerResponse> callback = new AsyncCallback<ServerResponse> () {
					
					@Override
					public void onSuccess(ServerResponse result) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				};
				String msg = "Disconnect" + Delimiters.MSG_PARTS + 
				SessionData.getInstance().getCurrentUser();
				forumSvc.sendMsg(msg, callback);
			}
		});
	}
}
